#include "alumnomaestro.h"
#include "ui_alumnomaestro.h"
#include "sqlConnection.h"
#include "mainwindow.h"
#include <iostream>
#include <stdlib.h>

AlumnoMaestro::AlumnoMaestro(QWidget *parent) :
    QTabWidget(parent),
    ui(new Ui::AlumnoMaestro)
{
    //THE PROGRAM SHOULD NEVER REACH THIS POINT!
    std::cout<<"El programa no deberia entrar aqui! que pedo? xD"<<std::endl;
    ui->setupUi(this);
    myQuery = new QSqlQuery(this->db);

    /*
    if(this->getTipo() == 3)
    {
        this->removeTab(0);
        this->setCurrentIndex(0);
    }
    else
    {
        this->removeTab(1);
        this->setCurrentIndex(0);
    }
    */
}

AlumnoMaestro::AlumnoMaestro(QWidget *parent, int value, int userID) :
    QTabWidget(parent),
    ui(new Ui::AlumnoMaestro)
{
    ui->setupUi(this);
    myQuery = new QSqlQuery(this->db);
    this->tabla = new QSqlQueryModel(this);

    //std::cout<<value<<std::endl;
    if(value == 2)
    {
        this->removeTab(0);
        this->setCurrentIndex(0);
        this->removeTab(1);

    }
    else
    {
        this->removeTab(1);
        this->removeTab(1);
        this->setCurrentIndex(0);

        id=userID;
        this->inscriptionBoxData = new QSqlQueryModel(this);
        ui->inscripcionTable->setModel(inscriptionBoxData);
        ui->inscripcionTable->show();

        this->tabla->setQuery("SELECT * FROM users WHERE id=" + QString::number(userID));
        ui->view_Nombre_textbox->setText((tabla->data(tabla->index(0,1),Qt::DisplayRole)).toString());
        ui->view_Fecha_textbox->setText((tabla->data(tabla->index(0,4),Qt::DisplayRole)).toString());
        ui->view_Direccion_textbox->setText((tabla->data(tabla->index(0,2),Qt::DisplayRole)).toString());
        ui->view_Matricula_textbox->setText((tabla->data(tabla->index(0,0),Qt::DisplayRole)).toString());

        this->tabla->setQuery("SELECT * FROM plan WHERE id="+ (tabla->data(tabla->index(0,6),Qt::DisplayRole)).toString());
        ui->view_Plan_textbox->setText((tabla->data(tabla->index(0,1),Qt::DisplayRole)).toString());

        this->tabla->setQuery("SELECT * FROM users WHERE id=" + QString::number(userID));
        this->tabla->setQuery("SELECT * FROM carrera WHERE id="+ (tabla->data(tabla->index(0,3),Qt::DisplayRole)).toString());
        ui->view_Carrera_textbox->setText((tabla->data(tabla->index(0,1),Qt::DisplayRole)).toString());

        this->classData = new QSqlQueryModel(this);
        classData->setQuery("SELECT mat.nombre as materia, g.id as id, hr.description as Horario "
                            +QString(" ,g.clave as grupo,tch.nombre as maestro, pl.nombre as plan, ")
                            +QString(" cr.nombre as carrera ")
                            +QString(" FROM grupos g INNER JOIN horas hr ON hr.id = g.hora_id ")
                            +QString(" INNER JOIN materias mat ON mat.id = g.materia_id ")
                            +QString(" INNER JOIN users tch ON tch.id = g.maestro_id ")
                            +QString(" INNER JOIN plan pl ON pl.id = g.plan_id ")
                            +QString(" INNER JOIN carrera cr ON cr.id = g.carrera_id WHERE g.deleted = false AND cr.id = "
                                     +(tabla->data(tabla->index(0,0),Qt::DisplayRole)).toString())
                            +QString(" AND g.id NOT IN (SELECT grupo_id FROM alumno_materias WHERE alumno_id ="+QString::number(id)+")"));

        ui->classComboBox->setModel(this->classData);
        ui->classComboBox->show();

        kardexData = new QSqlQueryModel(this);
        kardexData->setQuery("SELECT m.semestre as semestre ,m.nombre as materia ,k.primera "
                             +QString(" ,k.segunda,k.tercera,k.cuarta,k.quinta ")
                             +QString(" FROM users u ")
                             +QString(" INNER JOIN kardex k ON u.id = k.alumno_id ")
                             +QString(" INNER JOIN materias m ON m.id = k.materia_id WHERE TRUE AND u.id = "+ ui->view_Matricula_textbox->text()) );
        std::cout<<"yes"<<std::endl;

        ui->view_Kardex_tableView->setModel(kardexData);
        ui->view_Kardex_tableView->show();

        scheduleTableData = new QSqlQueryModel(this);
        scheduleTableData->setQuery("SELECT mt.nombre as Materia, hr.description as Horario"
                                    +QString(" FROM grupos g ")
                                    +QString(" INNER JOIN alumno_materias am ON am.grupo_id = g.id ")
                                    +QString(" INNER JOIN materias mt ON g.materia_id = mt.id ")
                                    +QString(" INNER JOIN horas hr ON hr.id = g.hora_id ")
                                    +QString(" WHERE am.alumno_id = "+QString::number(id)));
        ui->view_Horario_tableView->setModel(scheduleTableData);
        ui->view_Horario_tableView->show();

    }
}

AlumnoMaestro::~AlumnoMaestro()
{
    delete ui;
}

void AlumnoMaestro::changeEvent(QEvent *e)
{
    QTabWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void AlumnoMaestro::on_alumnoActualizar_clicked()
{
    this->tabla->setQuery("UPDATE users SET direccion = '"+ui->view_Direccion_textbox->text()+"' WHERE id ="+ QString::number(id));
    this->tabla->setQuery("SELECT direccion FROM users WHERE id ="+ QString::number(id));
    ui->view_Direccion_textbox->setText((tabla->data(tabla->index(0,0),Qt::DisplayRole)).toString());

}

void AlumnoMaestro::on_alumnoInscripcion_clicked()
{
    this->removeTab(0);
    this->addTab(ui->tabInscription,"Inscripcion");
    this->setCurrentIndex(0);
    this->resize(640,280);
    ((MainWindow*)this->parent())->resize(640,280);
}

void AlumnoMaestro::on_cancelInscripcion_clicked()
{
    this->removeTab(0);
    this->addTab(ui->alumnoTab,"Alumno");
    setCurrentIndex(0);
    this->resize(640,480);
    ((MainWindow*)this->parent())->resize(640,480);
    scheduleTableData->setQuery("SELECT mt.nombre as Materia, hr.description as Horario"
                                +QString(" FROM grupos g ")
                                +QString(" INNER JOIN alumno_materias am ON am.grupo_id = g.id ")
                                +QString(" INNER JOIN materias mt ON g.materia_id = mt.id ")
                                +QString(" INNER JOIN horas hr ON hr.id = g.hora_id ")
                                +QString(" WHERE am.alumno_id = "+QString::number(id)));
}

void AlumnoMaestro::on_classComboBox_currentIndexChanged(int index)
{
    this->grupoId = classData->data(classData->index(index,1),Qt::DisplayRole).toInt();
    this->inscriptionBoxData->setQuery("SELECT mat.nombre as materia, g.id as id, hr.description as Horario "
                                       +QString(" ,tch.nombre as maestro ")
                                       +QString(" FROM grupos g INNER JOIN horas hr ON hr.id = g.hora_id ")
                                       +QString(" INNER JOIN materias mat ON mat.id = g.materia_id ")
                                       +QString(" INNER JOIN users tch ON tch.id = g.maestro_id ")
                                       +QString(" INNER JOIN carrera cr ON cr.id = g.carrera_id WHERE g.deleted = false AND cr.id = "
                                                +(tabla->data(tabla->index(0,0),Qt::DisplayRole)).toString())
                                       +QString(" AND g.id = "+QString::number(grupoId)));

}

void AlumnoMaestro::on_newInscripcion_clicked()
{
    myQuery->exec("INSERT INTO alumno_materias VALUES(nextval('class_id'), "
                  +QString::number(this->grupoId)+", " + QString::number(id)+");");
    classData->setQuery("SELECT mat.nombre as materia, g.id as id, hr.description as Horario "
                        +QString(" ,g.clave as grupo,tch.nombre as maestro, pl.nombre as plan, ")
                        +QString(" cr.nombre as carrera ")
                        +QString(" FROM grupos g INNER JOIN horas hr ON hr.id = g.hora_id ")
                        +QString(" INNER JOIN materias mat ON mat.id = g.materia_id ")
                        +QString(" INNER JOIN users tch ON tch.id = g.maestro_id ")
                        +QString(" INNER JOIN plan pl ON pl.id = g.plan_id ")
                        +QString(" INNER JOIN carrera cr ON cr.id = g.carrera_id WHERE g.deleted = false AND cr.id = "
                                 +(tabla->data(tabla->index(0,0),Qt::DisplayRole)).toString())
                        +QString(" AND g.id NOT IN (SELECT grupo_id FROM alumno_materias WHERE alumno_id ="+QString::number(id)+")"));
}
